<template>
  <div>
    <!-- 工作日历, 年和月 -->
    <div class="select-box">
      <el-select
        v-model="currentYear"
        size="small"
        style="width: 120px; margin-right: 10px"
        @change="change"
      >
        <!-- 年份取给定年份的, 前五年 + 后五年 -->
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="currentMonth"
        size="small"
        style="width: 120px;"
        @change="change"
      >
        <el-option
          v-for="item in 12"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </div>
    <!-- 日历 -->
    <el-calendar v-model="current">
      <template v-slot:dateCell="{date, data}">
        <div class="date-content">
          <span class="text" :class="{current: data.isSelected}">{{ date.getDate() }}</span>
          <span v-if="showRest(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'WorkCalendar',
  data() {
    return {
      currentYear: new Date().getFullYear(), // 默认显示当前年
      currentMonth: new Date().getMonth() + 1, // 默认显示当前月
      current: new Date()
    }
  },
  computed: {
    yearList() {
      return Array.from(new Array(11), (item, index) => this.currentYear + index - 5)
    }
  },
  watch: {
    // 监视日历数据 同步下拉框年月
    current: {
      deep: true,
      handler(value) {
        this.currentYear = value.getFullYear()
        this.currentMonth = value.getMonth() + 1
      }
    }
  },
  methods: {
    // 改变下拉框中年月时同步改变日历绑定的数据
    change() {
      this.current = new Date(this.currentYear, this.currentMonth - 1) // 注意-1
    },
    // 是否显示休息标识
    showRest(date) {
      const day = date.getDay()
      // 判断是否是周末
      if (day === 0 || day === 6) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.select-box {
  display: flex;
  justify-content: flex-end;
}
// 隐藏日历头部
::v-deep .el-calendar__header {
   display: none
 }

::v-deep .el-calendar-day {
  height:  auto;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 22px;
  height: 22px;
  line-height: 22px;
 display: inline-block;

}
.current{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
</style>
